package cn.gok.controller;

import cn.gok.pojo.User;
import cn.gok.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/user/")
public class UserController extends  BaseController{
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public JSONObject login(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        User user=userService.login(json.getString("username"),json.getString("password"));
        if(user==null){
            res.put(MSG,"登录失败");
        }else{
            res.put("user",user);
            res.put("msg","登录成功");
        }
        return res;
    }

    @RequestMapping("/registered")
    @ResponseBody
    public JSONObject registered(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        res.put("code",userService.registered(json.getString("username"),json.getString("password")));
        return res;
    }
//    管理员查询所有用户
    @RequestMapping("/selall")
    @ResponseBody
    public JSONObject selall(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        res.put("data",userService.selall());
        return res;
    }
//    查看所有权限
    @RequestMapping("/seldentity")
    @ResponseBody
    public JSONObject seldentity(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        res.put("data",userService.selalldentity());
        return res;
    }
//    管理员根据用户名查询单个人的信息
    @RequestMapping("/selof")
    @ResponseBody
    public JSONObject selof(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        res.put("data",userService.selof(json.getString("query")));
        return res;
    }
//    管理员编辑用户信息（包括权限管理）
    @RequestMapping("/change")
    @ResponseBody
    public JSONObject change(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        User user=new User();
        user.setId(json.getInteger("id"));
        user.setUsername(json.getString("username"));
        user.setPassword(json.getString("password"));
        user.setDentity_id(json.getInteger("dentity_id"));
        res.put("code",userService.change(user));
        return res;
    }
//    删除用户
    @RequestMapping("/del")
    @ResponseBody
    public JSONObject del(@RequestBody JSONObject json){
        JSONObject res=new JSONObject();
        res.put("code",userService.del(json.getString("query")));
        return res;
    }

}
